import {useState} from "react";

// 查询所有员工考勤信息响应接口
interface UseAllAttendanceResponse {
    code: number;
    data: string;
    message: string;
}

// useCheckAllAttendance hook接口
interface UseCheckAllAttendance {
    checkAllAttendance: () => Promise<void>;
    data: string;
    error: string | null;
}

export function useCheckAllAttendance(): UseCheckAllAttendance {
    const [data, setData] = useState('');
    const [error, setError] = useState<string | null>(null);
    const token = localStorage.getItem('token');

    const checkAllAttendance = async (): Promise<void> => {
        setError(null);
        try {
            if (!token) {
                setError('未提供授权令牌');
                return;
            }
            const response = await fetch('http://localhost:6868/admin/checkAttendance', {
                method: 'GET',
                headers: {
                    'Authorization': token!
                },
            });
            if (!response.ok) {
                throw new Error("查询所有员工考勤信息失败");
            }
            const responseData: UseAllAttendanceResponse = await response.json();
            setData(responseData.data);
        } catch (error) {
            setError((error as Error).message);
        }
    };

    return {checkAllAttendance, data, error};
}
